#include "monsterquery.h"
#include<QVector>
#include<QPainter>

monsterQuery::monsterQuery(QWidget *parent) : QWidget(parent)
{

}

monsterQuery::monsterQuery(monster m[][11],hero h)
{
    setFixedSize(600,550);
    setWindowTitle("圣光徽");
    update();

    for(int i=0;i<11;i++){
        for(int j=0;j<11;j++){
            monsters[i][j]=m[i][j];
        }
    }
    heros=h;
    update();
}

void monsterQuery::paintEvent(QPaintEvent *)
{
    int num=0;
    QPainter paint(this);
    QVector<int> p;
    for(int i=0;i<11;i++){
        for(int j=0;j<11;j++){
            if(monsters[i][j]._type!=0&&!monsters[i][j]._isdead){
                if(judge(p,monsters[i][j]._type))
                    continue;
                else {
                    paint.drawPixmap(0,num*60,monsters[i][j].monsters);
                    int temp=hurt(heros,monsters[i][j]);
                    QString part;
                    if(temp==-1){
                        part="不可攻击";
                    }
                    else{
                        part=QString::number(temp,10);
                    }
                    paint.drawText(QPoint(100,30+num*60),"血量："+QString::number(monsters[i][j]._Hp,10)
                                                                   +",攻击："+QString::number(monsters[i][j]._Attack,10)
                                                                   +",防御："+QString::number(monsters[i][j]._defend,10)
                                                                   +",损失血量："+part);
                    num++;
                    p.push_back(monsters[i][j]._type);
                }
            }
        }
    }
}

void monsterQuery::keyPressEvent(QKeyEvent *event)
{
    switch(event->key()){
    case Qt::Key_H:
        this->hide();
        break;
    }
}

bool monsterQuery::judge(QVector<int> &p, int num)
{
    for(int i=0;i<p.size();i++){
        if(p[i]==num)
            return true;
    }
    return false;
}

int monsterQuery::hurt(hero h, monster m)
{
    if(h._attack<=m._defend)
        return -1;
    int temp=0;
    int temp1=m._Hp;
    temp+=m._magicHurt;

    if(m._type==17){
        temp+=0.25*h._hp;
    }

    if(m._type==26){
        temp+=h._hp/3;
    }

    if(m._Attack<=h._defend)
        return temp;

    while(temp1>0){
        temp1-=h._attack-m._defend;
        if(temp1<=0)
            break;
        temp+=m._Attack-h._defend;
    }
    if(temp<=0)
        return 0;
    return temp;
}
